/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.atlasmobile.manager;

import br.com.foxline.enums.TypePatio;
import br.com.foxline.model.Bairro;
import br.com.foxline.model.City;
import br.com.foxline.model.Dne;
import br.com.foxline.service.BairroService;
import br.com.foxline.service.CityService;
import br.com.foxline.service.DneService;
import br.com.foxline.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author laverson
 */
@ManagedBean
@ViewScoped
public class ManagerDne implements Serializable {

    @EJB
    DneService dneService;
    @EJB
    BairroService bairroService;
    @EJB
    CityService cityService;
    private List<Dne> dneResult;
    private Dne dne;
    private Dne deleteDne;
    private String vDne;
    private City city;
    private Bairro bairro;
    private TypePatio typePatio;

    @PostConstruct
    public void init() {
        dne = new Dne();
        dne.setCity(new City());
        dne.setBairro(new Bairro());

        city = new City();
        bairro = new Bairro();
        typePatio = null;

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        vDne = params.get("vDne");
        paramPost();
    }

    public void paramPost() {
        try {
            if (vDne != null) {
                dne = dneService.find(Long.parseLong(vDne));
                if (dne.getCity() != null && dne.getCity().getId() != null) {
                    city = cityService.find(dne.getCity().getId());
                }
                if (dne.getBairro() != null && dne.getBairro().getId() != null) {
                    bairro = bairroService.find(dne.getBairro().getId());
                }
                typePatio = dne.getTypePatio();
            }
        } catch (Exception erro) {
            erro.printStackTrace();
        }
    }

    public void save() {
        try {
            if (city.getId() != null) {
                dne.setCity(city);
            }
            if (bairro.getId() != null) {
                dne.setBairro(bairro);
            }
            dne.setTypePatio(typePatio);
            dne.setNomeclog(typePatio.name().concat(" ").concat(dne.getPatio()));
            dne.setNomeslog(dne.getPatio());
            dneService.save(dne);
            Msg.messagemInfo(Msg.SuccessFull);
        } catch (Exception e) {
            e.printStackTrace();
            Msg.messagemError(Msg.Failure);
        }
        init();
    }

    public Bairro getBairro() {
        return bairro;
    }

    public void setBairro(Bairro bairro) {
        this.bairro = bairro;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public List<Bairro> autocompleteBairro(String query) {
        return bairroService.findByName(query, city);
    }

    public void setarCidade() {
        if (city.getName() == null) {
            bairro = new Bairro();
        }
    }

    public void clearBairro() {
        if (city.getName() == null) {
            bairro = new Bairro();
        }
    }

    public List<City> autocompleteCity(String query) {
        return cityService.autoComplete(query);
    }

    public void search() {
        dne.setCity(city);
        dne.setBairro(bairro);
        dneResult = dneService.findDne(dne);
    }

    public void update() {
        try {
            dne.setBairro(bairro);
            dne.setCity(city);
            dne.setTypePatio(typePatio);
            dne.setNomeslog(dne.getPatio());
            dne.setNomeclog(typePatio.name().concat(" ").concat(dne.getPatio()));
            dneService.update(dne);
            city = cityService.find(city.getId());
            bairro = bairroService.find(bairro.getId());
            Msg.messagemInfo(Msg.SuccessFull);
        } catch (Exception e) {
            e.printStackTrace();
            Msg.messagemError(Msg.Failure);
        }
    }

    public void delete() {
        dneService.delete(deleteDne);
        dneResult = new ArrayList<Dne>();
    }

    public void clear() {
        dne = new Dne();
        city = new City();
        bairro = new Bairro();
    }

    public List<TypePatio> getTypePatioList() {
        List<TypePatio> items = Arrays.asList(TypePatio.values());
        return items;
    }

    public Dne getDne() {
        return dne;
    }

    public void setDne(Dne dne) {
        this.dne = dne;
    }

    public List<Dne> getDneResult() {
        return dneResult;
    }

    public void setDneResult(List<Dne> dneResult) {
        this.dneResult = dneResult;
    }

    public Dne getDeleteDne() {
        return deleteDne;
    }

    public void setDeleteDne(Dne deleteDne) {
        this.deleteDne = deleteDne;
    }

    public TypePatio getTypePatio() {
        return typePatio;
    }

    public void setTypePatio(TypePatio typePatio) {
        this.typePatio = typePatio;
    }
}
